Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.6
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.6, n = 366)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.05556 13.05070 13.04592 13.04120 13.03655 13.03196 13.02744 13.02298
## [9] 13.01858 13.01424 13.00996 13.00573 13.00155 12.99742 12.99333 12.98930
## [17] 12.98530 12.98135 12.97744 12.97356 12.96972 12.96591 12.96214 12.95839
## [25] 12.95467 12.95097 12.94730 12.94365 12.94002 12.93640 12.93280 12.92921
## [33] 12.92564 12.92207 12.91850 12.91495 12.91140 12.90786 12.90434 12.90084
## [41] 12.89736 12.89390 12.89047 12.88708 12.88371 12.88039 12.87710 12.87385
## [49] 12.87065 12.86750 12.86439 12.86134 12.85835 12.85541 12.85254 12.84973
## [57] 12.84699 12.84432 12.84172 12.83920 12.83676 12.83440 12.83212 12.82993
## [65] 12.82783 12.82583 12.82392 12.82210 12.82039 12.81879 12.81729 12.81587
## [73] 12.81448 12.81313 12.81183 12.81056 12.80934 12.80816 12.80702 12.80593
## [81] 12.80489 12.80389 12.80294 12.80203 12.80118 12.80038 12.79963 12.79893
## [89] 12.79828 12.79769 12.79716 12.79668 12.79625 12.79589 12.79558 12.79533
## [97] 12.79515 12.79502 12.79496 12.79496 12.79503 12.79516 12.79536 12.79562
## [105] 12.79595 12.79636 12.79683 12.79737 12.79798 12.79867 12.79943 12.80027
## [113] 12.80118 12.80217 12.80323 12.80437 12.80560 12.80690 12.80828 12.80975
## [121] 12.81130 12.81293 12.81465 12.81645 12.81834 12.82032 12.82238 12.82454
## [129] 12.82678 12.82912 12.83155 12.83407 12.83669 12.83940 12.84221 12.84605
## [137] 12.85173 12.85907 12.86788 12.87798 12.88917 12.90129 12.91414 12.92753
## [145] 12.94128 12.95520 12.96912 12.98284 12.99618 13.00896 13.02098 13.03207
## [153] 13.04203 13.05069 13.05786 13.06335 13.06925 13.07758 13.08804 13.10034
## [161] 13.11417 13.12923 13.14524 13.16187 13.17885 13.19587 13.21263 13.22884
## [169] 13.24419 13.25839 13.27114 13.28213 13.29108 13.29768 13.30163 13.30399
## [177] 13.30599 13.30764 13.30893 13.30986 13.31043 13.31063 13.31045 13.30991
## [185] 13.30899 13.30769 13.30601 13.30394 13.30148 13.29864 13.29539 13.29175
## [193] 13.28771 13.28327 13.27842 13.27316 13.26748 13.26139 13.25488 13.24795
## [201] 13.24060 13.23281 13.22460 13.21595 13.20686 13.19733 13.18736 13.17694
## [209] 13.16607 13.15475 13.14298 13.13074 13.11804 13.10488 13.09125 13.07715
## [217] 13.06258 13.04752 13.03199 13.01445 12.99361 12.96980 12.94336 12.91462
## [225] 12.88391 12.85157 12.81794 12.78334 12.74812 12.71261 12.67715 12.64206
## [233] 12.60768 12.57436 12.54242 12.51219 12.48402 12.45823 12.43517 12.41516
## [241] 12.39641 12.37695 12.35689 12.33634 12.31541 12.29419 12.27280 12.25135
## [249] 12.22993 12.20865 12.18763 12.16697 12.14676 12.12713 12.10818 12.09000
## [257] 12.07272 12.05643 12.04124 12.02720 12.01426 12.00236 11.99144 11.98144
## [265] 11.97232 11.96400 11.95643 11.94956 11.94333 11.93768 11.93256 11.92789
## [273] 11.92364 11.91974 11.91613 11.91276 11.90957 11.90650 11.90349 11.90050
## [281] 11.89745 11.89429 11.89097 11.88743 11.88361 11.87945 11.87490 11.86989
## [289] 11.86438 11.85830 11.85160 11.84422 11.83610 11.82718 11.81795 11.80889
## [297] 11.79999 11.79122 11.78257 11.77400 11.76549 11.75703 11.74859 11.74015
## [305] 11.73169 11.72317 11.71459 11.70591 11.69712 11.68819 11.67910 11.66983
## [313] 11.66035 11.65065 11.64069 11.63067 11.62078 11.61099 11.60127 11.59159
## [321] 11.58194 11.57227 11.56256 11.55280 11.54294 11.53296 11.52284 11.51254
## [329] 11.50205 11.49133 11.48036 11.46921 11.45796 11.44662 11.43519 11.42367
## [337] 11.41206 11.40035 11.38856 11.37667 11.36470 11.35264 11.34049 11.32826
## [345] 11.31594 11.30353 11.29104 11.27846 11.26579 11.25305 11.24022 11.22731
## [353] 11.21431 11.20124 11.18808 11.17484 11.16153 11.14813 11.13465 11.12110
## [361] 11.10747 11.09376 11.07997 11.06611 11.05218 11.03816
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.6, n = 366)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.62165 12.61714 12.61272 12.60839 12.60414 12.59999 12.59593 12.59195
## [9] 12.58807 12.58426 12.58055 12.57692 12.57337 12.56991 12.56653 12.56324
## [17] 12.56003 12.55689 12.55384 12.55087 12.54797 12.54516 12.54242 12.53976
## [25] 12.53718 12.53467 12.53223 12.52987 12.52758 12.52537 12.52323 12.52116
## [33] 12.51916 12.51723 12.51536 12.51357 12.51184 12.51018 12.50859 12.50706
## [41] 12.50560 12.50421 12.50290 12.50165 12.50049 12.49940 12.49838 12.49745
## [49] 12.49659 12.49582 12.49513 12.49453 12.49401 12.49358 12.49324 12.49299
## [57] 12.49283 12.49276 12.49279 12.49291 12.49314 12.49346 12.49388 12.49440
## [65] 12.49503 12.49575 12.49659 12.49753 12.49858 12.49975 12.50102 12.50244
## [73] 12.50404 12.50583 12.50778 12.50990 12.51219 12.51463 12.51723 12.51998
## [81] 12.52287 12.52590 12.52906 12.53236 12.53577 12.53931 12.54297 12.54673
## [89] 12.55061 12.55458 12.55865 12.56281 12.56705 12.57138 12.57579 12.58027
## [97] 12.58481 12.58942 12.59409 12.59881 12.60357 12.60839 12.61324 12.61812
## [105] 12.62303 12.62797 12.63293 12.63790 12.64289 12.64788 12.65286 12.65785
## [113] 12.66283 12.66803 12.67366 12.67969 12.68606 12.69274 12.69969 12.70685
## [121] 12.71420 12.72168 12.72925 12.73688 12.74451 12.75211 12.75963 12.76703
## [129] 12.77427 12.78131 12.78810 12.79460 12.80077 12.80656 12.81194 12.81781
## [137] 12.82505 12.83350 12.84302 12.85346 12.86469 12.87656 12.88893 12.90165
## [145] 12.91458 12.92758 12.94050 12.95320 12.96554 12.97738 12.98856 12.99896
## [153] 13.00842 13.01680 13.02395 13.02975 13.03642 13.04607 13.05833 13.07283
## [161] 13.08922 13.10712 13.12618 13.14602 13.16629 13.18661 13.20663 13.22598
## [169] 13.24429 13.26120 13.27635 13.28936 13.29989 13.30755 13.31199 13.31417
## [177] 13.31535 13.31553 13.31475 13.31304 13.31044 13.30696 13.30263 13.29750
## [185] 13.29157 13.28489 13.27749 13.26938 13.26061 13.25119 13.24116 13.23055
## [193] 13.21939 13.20770 13.19551 13.18286 13.16977 13.15627 13.14239 13.12815
## [201] 13.11360 13.09875 13.08364 13.06829 13.05273 13.03700 13.02111 13.00511
## [209] 12.98902 12.97286 12.95667 12.94048 12.92431 12.90819 12.89216 12.87624
## [217] 12.86046 12.84484 12.82943 12.81276 12.79353 12.77202 12.74851 12.72326
## [225] 12.69654 12.66862 12.63979 12.61031 12.58045 12.55048 12.52069 12.49133
## [233] 12.46268 12.43501 12.40860 12.38371 12.36063 12.33961 12.32094 12.30488
## [241] 12.29031 12.27593 12.26174 12.24775 12.23397 12.22039 12.20704 12.19392
## [249] 12.18102 12.16836 12.15595 12.14378 12.13188 12.12023 12.10885 12.09775
## [257] 12.08693 12.07640 12.06616 12.05666 12.04829 12.04098 12.03466 12.02929
## [265] 12.02478 12.02109 12.01813 12.01585 12.01419 12.01308 12.01245 12.01225
## [273] 12.01240 12.01285 12.01352 12.01436 12.01530 12.01628 12.01723 12.01809
## [281] 12.01879 12.01927 12.01946 12.01931 12.01874 12.01770 12.01611 12.01392
## [289] 12.01106 12.00746 12.00307 11.99781 11.99163 11.98445 11.97686 11.96946
## [297] 11.96221 11.95509 11.94806 11.94110 11.93418 11.92727 11.92033 11.91335
## [305] 11.90628 11.89910 11.89178 11.88430 11.87661 11.86870 11.86053 11.85207
## [313] 11.84329 11.83417 11.82467 11.81503 11.80550 11.79603 11.78659 11.77715
## [321] 11.76769 11.75815 11.74852 11.73875 11.72882 11.71870 11.70834 11.69771
## [329] 11.68679 11.67554 11.66393 11.65203 11.63996 11.62771 11.61530 11.60270
## [337] 11.58994 11.57701 11.56391 11.55064 11.53721 11.52361 11.50985 11.49592
## [345] 11.48184 11.46759 11.45319 11.43863 11.42391 11.40903 11.39401 11.37883
## [353] 11.36350 11.34802 11.33239 11.31661 11.30069 11.28462 11.26841 11.25205
## [361] 11.23555 11.21892 11.20214 11.18522 11.16817 11.15099
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.6, n = 366)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.06118 12.05430 12.04754 12.04089 12.03434 12.02791 12.02158 12.01536
## [9] 12.00924 12.00322 11.99730 11.99148 11.98575 11.98012 11.97457 11.96912
## [17] 11.96376 11.95847 11.95328 11.94816 11.94313 11.93817 11.93329 11.92849
## [25] 11.92375 11.91909 11.91450 11.90997 11.90551 11.90111 11.89677 11.89249
## [33] 11.88827 11.88411 11.87999 11.87593 11.87192 11.86796 11.86404 11.86017
## [41] 11.85634 11.85255 11.84880 11.84511 11.84149 11.83795 11.83450 11.83113
## [49] 11.82784 11.82464 11.82153 11.81851 11.81558 11.81275 11.81001 11.80737
## [57] 11.80483 11.80238 11.80005 11.79781 11.79568 11.79366 11.79175 11.78994
## [65] 11.78826 11.78668 11.78522 11.78388 11.78267 11.78157 11.78059 11.77974
## [73] 11.77902 11.77842 11.77796 11.77763 11.77743 11.77736 11.77740 11.77752
## [81] 11.77770 11.77797 11.77831 11.77874 11.77926 11.77986 11.78056 11.78135
## [89] 11.78223 11.78322 11.78431 11.78550 11.78680 11.78821 11.78973 11.79137
## [97] 11.79313 11.79501 11.79701 11.79914 11.80140 11.80379 11.80632 11.80898
## [105] 11.81178 11.81473 11.81782 11.82106 11.82445 11.82799 11.83169 11.83555
## [113] 11.83958 11.84376 11.84812 11.85264 11.85733 11.86221 11.86725 11.87248
## [121] 11.87790 11.88350 11.88929 11.89527 11.90144 11.90781 11.91438 11.92116
## [129] 11.92814 11.93532 11.94272 11.95033 11.95816 11.96620 11.97447 11.98433
## [137] 11.99696 12.01210 12.02947 12.04879 12.06979 12.09219 12.11572 12.14011
## [145] 12.16507 12.19034 12.21565 12.24071 12.26525 12.28899 12.31167 12.33300
## [153] 12.35272 12.37055 12.38621 12.39942 12.41345 12.43142 12.45283 12.47721
## [161] 12.50408 12.53295 12.56335 12.59478 12.62678 12.65885 12.69051 12.72128
## [169] 12.75069 12.77824 12.80346 12.82586 12.84496 12.86028 12.87134 12.87950
## [177] 12.88649 12.89232 12.89703 12.90065 12.90321 12.90473 12.90525 12.90479
## [185] 12.90339 12.90108 12.89788 12.89382 12.88895 12.88327 12.87683 12.86965
## [193] 12.86177 12.85321 12.84400 12.83418 12.82376 12.81279 12.80129 12.78930
## [201] 12.77683 12.76392 12.75061 12.73691 12.72286 12.70849 12.69384 12.67892
## [209] 12.66376 12.64841 12.63289 12.61722 12.60144 12.58557 12.56966 12.55371
## [217] 12.53778 12.52188 12.50604 12.48864 12.46822 12.44509 12.41957 12.39196
## [225] 12.36259 12.33175 12.29977 12.26695 12.23361 12.20006 12.16661 12.13356
## [233] 12.10124 12.06996 12.04002 12.01174 11.98543 11.96140 11.93996 11.92143
## [241] 11.90454 11.88781 11.87125 11.85486 11.83864 11.82260 11.80673 11.79104
## [249] 11.77553 11.76020 11.74506 11.73010 11.71534 11.70076 11.68638 11.67219
## [257] 11.65820 11.64441 11.63082 11.61769 11.60526 11.59349 11.58235 11.57180
## [265] 11.56182 11.55237 11.54342 11.53494 11.52689 11.51925 11.51197 11.50503
## [273] 11.49839 11.49203 11.48590 11.47999 11.47424 11.46864 11.46314 11.45773
## [281] 11.45235 11.44699 11.44161 11.43617 11.43065 11.42501 11.41921 11.41324
## [289] 11.40704 11.40060 11.39388 11.38684 11.37946 11.37169 11.36389 11.35639
## [297] 11.34917 11.34219 11.33543 11.32887 11.32246 11.31620 11.31004 11.30396
## [305] 11.29793 11.29192 11.28591 11.27987 11.27376 11.26757 11.26126 11.25481
## [313] 11.24818 11.24135 11.23430 11.22721 11.22030 11.21353 11.20688 11.20033
## [321] 11.19385 11.18742 11.18102 11.17461 11.16817 11.16169 11.15513 11.14846
## [329] 11.14168 11.13474 11.12764 11.12042 11.11319 11.10594 11.09868 11.09139
## [337] 11.08410 11.07679 11.06947 11.06214 11.05480 11.04745 11.04010 11.03274
## [345] 11.02537 11.01800 11.01064 11.00327 10.99590 10.98853 10.98117 10.97381
## [353] 10.96646 10.95911 10.95177 10.94445 10.93713 10.92982 10.92253 10.91525
## [361] 10.90799 10.90075 10.89352 10.88631 10.87912 10.87196
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")